﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Dal.Models;
using Framework.Common;
using Dal.Entities;
using Framework.Common.LinqExtention;

namespace Web.Controllers
{
    public class SENDQuery
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public DateTime? BeginTime { get; set; }
        public DateTime? EndTime { get; set; }
        public int State { get; set; }
    }

    [ValidateInput(false)]
    public class MsgController : Controller
    {
        //
        // GET: /Msg/

        public ActionResult Index()
        {
            SENDQuery model = new SENDQuery();
            TryUpdateModel(model);
            int pageindex = TypeConvertHelper.StrToInt(Request["pageindex"], 1);
            int recordcount;
            string owner = Common.GetUserName();
            IList<SEND> list = null;
            Expression<Func<SEND, bool>> exp = m => m.OWNER == owner;
            if (!string.IsNullOrWhiteSpace(model.Name))
            {
                exp = exp.And(i => i.NAME == model.Name);
            }
            if (!string.IsNullOrWhiteSpace(model.Phone))
            {
                exp = exp.And(i => i.PHONE == model.Phone);
            }
            if (model.BeginTime.HasValue)
            {
                string beginTime = model.BeginTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                exp = exp.And(i => i.STIME.CompareTo(beginTime) <= 0);
            }
            if (model.EndTime.HasValue)
            {
                string endTime = model.EndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                exp = exp.And(i => i.STIME.CompareTo(endTime) >= 0);
            }
            if (model.State > 0)
            {
                exp = exp.And(i => i.STATE == model.State.ToString());
            }
            Framework.Data.IRepository rep = Framework.Data.RepositoryFactory.GetRepository();
            list = rep.GetPaged<SEND, int>(
                pageindex - 1,
                10,
                out recordcount,
                exp,
                i => i.ID,
                true);

            ViewBag.RecordCount = recordcount;
            ViewBag.PageIndex = pageindex;
            ViewBag.List = list;
            return View(model);
        }


        public ActionResult Add()
        {
            string ids = Request["tongxun"];
            string msg = Request["msg"];
            string owner = Common.GetUserName();
            if (!string.IsNullOrWhiteSpace(ids) && !string.IsNullOrWhiteSpace(msg))
            {
                int[] idArray = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                   .Select(i => TypeConvertHelper.StrToInt(i)).ToArray();
                foreach (var id in idArray)
                {
                    if (id > 0)
                    {
                        using (MessageContext db = new MessageContext())
                        {
                            var chactor = db.CHACTORs.Where(i => i.ID == id).FirstOrDefault();
                            if (chactor != null)
                            {
                                WAITTEMP model = new WAITTEMP();
                                model.CONTENT = msg;
                                model.NAME = chactor.NAME;
                                model.OWNER = owner;
                                model.PHONE = chactor.PHONE;
                                model.STIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                model.PP = "";
                                model.STATE = "";
                                db.WAITTEMPs.Add(model);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                return Json(new { result = true }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { result = false }, JsonRequestBehavior.AllowGet);
        }

    }
}
